package org.gis.mvc.view;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.gis.mapserver.base.InjectionValidable;
import org.gis.mapserver.responsewriter.ResponseWriter;
import org.springframework.web.servlet.View;

/**
 *
 * @author gus
 */
public abstract class ViewBase implements View, InjectionValidable
{
    protected ResponseWriter writer;

    public void setWriter( ResponseWriter writer )
    {
        this.writer = writer;
    }

    public ResponseWriter getWriter()
    {
        return writer;
    }

    public void afterInitialize() throws Exception
    {
        if ( writer == null )
        {
            throw new Exception("not set writer");
        }
    }

    @Override
    public void render( Map model, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        try
        {
            doRender(model, request, response);
        } catch (Throwable t)
        {
            response.sendError(
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.valueOf(t));
        }
    }

    protected abstract void doRender( Map model, HttpServletRequest request, HttpServletResponse response ) throws Exception;
}
